<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.google.appengine.api.datastore.FetchOptions" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Query.FilterOperator" %>
<%@ page import="com.google.appengine.api.images.*" %>
<%@ page import="com.google.appengine.api.blobstore.BlobKey" %>

<!-- Redirect to the login page if there is no session created -->
<%
if (session.getAttribute("username") == null) {
	response.sendRedirect("/login.jsp");
}
	String keyword = request.getParameter("keyword").toString().toLowerCase();
	String logo="", default_prev="", label="";
	String MediaType = (String)request.getParameter("media_type");
	
	//Configuring images and link according to media type
	if(MediaType.equals("application")){
		logo="doc";
		label="document";
		default_prev="/images/doc.png";
	}
	
	if(MediaType.equals("video")){
		logo="video";
		label=logo;
		default_prev="/images/movie_default_preview.png";
	}
	
	if(MediaType.equals("audio")){
		logo="audio";
		label=logo;
		default_prev="/images/audio_default.png";
	}
	
	if(MediaType.equals("image")){
		logo="pic";
		label="Picture";
	}	

	
	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Key accountKey = KeyFactory.createKey("AudioFiles", "mediaFiles");
		int rawByteSize;
		Query query = new Query("Audio", accountKey).
				addFilter("user", FilterOperator.EQUAL, (String)session.getAttribute("username")).
				addSort("mediaName", Query.SortDirection.ASCENDING).
				addFilter("contentType", FilterOperator.EQUAL, MediaType);

		List<Entity> uploadedFiles = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(20));
		
		boolean emptyResults = false;
		%> 	<div id="Search_letter">
				<% if(keyword.equals("") || keyword.equals(null) || keyword.equals(" ")){
					%>Please Enter a keyword to search<%
				}
				else {%>
					 Searching for "<%=request.getParameter("keyword") %>"
				<% }
				%> </div> <%
			for (Entity file: uploadedFiles) {
				String lowerMediaName = file.getProperty("mediaName").toString().toLowerCase();
				String lowerFileName = file.getProperty("fileName").toString().toLowerCase();
				
				if ((lowerMediaName.indexOf(keyword) > -1) || (lowerFileName.indexOf(keyword)> -1)) {
					String keyParameter = KeyFactory.keyToString(file.getKey());
					emptyResults = true;
					
					//INITIALIZING IMAGE TYPE PARAMETERS
					if(MediaType.equals("image")){
						//GETING THE IMAGE PREVIEW
						BlobKey blobKey = (BlobKey) file.getProperty("blobKey");
						ImagesService image = ImagesServiceFactory.getImagesService();
						String imageURL = image.getServingUrl(blobKey, 300, true);

						default_prev=imageURL;
					}
				%> 	
				<div align="center" id="preview_file" onmouseover="showOptions(this)" onmouseout="hideOptions(this)">
					<div class="preview_doc"><img src="<%=default_prev%>" width="100%" height="100%"/></div>
					<div id="file_info">
						<p>Name:<%= file.getProperty("mediaName") %></p>
						<% if(file.getProperty("contentType").equals("video")){ %>
							<p>Genre :	<%= file.getProperty("genre") %></p>
							<p>Year :	<%= file.getProperty("year") %></p>
							<p>Collection : <%= file.getProperty("collection") %></p>
						<% } else if(file.getProperty("contentType").equals("audio")) { %>
							<p>Artist :	<%= file.getProperty("artist") %></p>
							<p>Album :	<%= file.getProperty("album") %></p>
							<p>Genre : <%= file.getProperty("genre") %></p>
						<% } else if(file.getProperty("contentType").equals("image")) { %>
							<p>Photo Album :	<%= file.getProperty("album") %></p>
							<p>Size :	<%= Integer.parseInt(file.getProperty("fileSize").toString())/1024 %> kB</p>
							<p>Date Created : <%= file.getProperty("dateCreated") %></p>
						<% } else { %>
							<p>Collection :	<%= file.getProperty("album") %></p>
							<p>Size :	<%= Integer.parseInt(file.getProperty("fileSize").toString())/1024 %> kB</p>
							<p>Date Created : <%= file.getProperty("dateCreated") %></p>
						<% } %>
						<p>Media Category : <%= file.getProperty("contentType") %></p> 
					</div>
					<div class="fileOptions">
						<a href="/download?entityKey=<%= keyParameter %>"><img src="images/download.png" alt="" /></a>
						<a href="/delete?entityKey=<%= keyParameter %>"><img src="images/trash_bin.png" alt="" /></a>
						<a href="#"><img src="images/upload.png" alt="" /></a>
					</div>
				</div>
				<%
				} 
			}
		
		//EMPTY SEARCH RESULTS MESSAGE
				if(emptyResults==false) {
					%>
					<div align="center" style="margin-top:100px;">
						<img src="/images/<%=logo%>.png" width="128" height="128"/> </br>
						<p style="font-size: 16px;">
							No <%=label%> files in the media vault under 
							<label style="font-weight: bold;">"<%=request.getParameter("keyword") %>"</label>
						</p>
					</div>
					<%
				}
%>